﻿using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

using NLog;

namespace CodingBlog.UI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Admin",
                "cb-admin",
                new { controller = "Admin", action = "Index" }
            );

            routes.MapRoute(
                "PostsListPerTag",
                "Tag/{Tag}",
                new { controller = "Post", action = "ListPerTag" }
            );

            routes.MapRoute(
                "InstallRoute",
                "Install/{action}",
                new { controller = "Install", action = "Index" }
            );

            routes.MapRoute(
                "PostDetailsByDateAndName",
                "{Year}/{Month}/{Day}/{PostName}",
                new { controller = "Post", action = "Details" },
                new { Year = @"\d{4}", Month = @"\d{1,2}", Day = @"\d{2}" }
            );

            routes.MapRoute(
                "Default",
                "{Year}/{Month}/{Day}",
                new { controller = "Post", action = "List", Year = UrlParameter.Optional, Month = UrlParameter.Optional, Day = UrlParameter.Optional }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            var logger = LogManager.GetCurrentClassLogger();
            logger.Info("The application has started");
        }
    }
}